﻿using MediatR;
using Traceability.Commont.Domains.ScratchTheFat;
using Traceability.Commont.DTOs.CommontFiles;
using Traceability.Commont.DTOs.ReportDTOs;
using Traceability.Commont.DTOs.WhiteStripWeighingHtmlGroup;
using Traceability.Commont.Infrastructures.Interfaces;

namespace U.Enterprise.Api.Applications.PartitionWarehousings
{
    /// <summary>
    /// 分割入库分组显示
    /// </summary>
    public class SpliteGroupHander : IRequestHandler<SpliteRUGroupCommand, Resqults<SpliteWightGroupDTO>>
    {
        private readonly IBateRepository<SplitRU> split;

        public SpliteGroupHander(IBateRepository<SplitRU> split)
        {
            this.split = split;
        }

        public async Task<Resqults<SpliteWightGroupDTO>> Handle(SpliteRUGroupCommand request, CancellationToken cancellationToken)
        {
            try
            {
                Resqults <SpliteWightGroupDTO> res=new Resqults<SpliteWightGroupDTO> ();
                int years = DateTime.Now.Year;
                int months = DateTime.Now.Month;
                if (request.Date!=null)
                {
                    years=request.Date.Value.Year;
                    months=request.Date.Value.Month;
                }
                var list = (from a in split.GetAll()
                           where (a.DocumentsDate.Year == years && a.DocumentsDate.Month == months)
                           group new { a} by new { a.DocumentsDate.Day} into c
                           select new SpliteWightGroupDTO()
                           {
                               Num = c.Sum(x => x.a.WeightNum),
                               Wight = c.Sum(x=>x.a.NetWeight* x.a.WeightNum),
                               Day=c.Key.Day
                           }).ToList();
                res.Date = list;
                return res;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
